﻿Imports Business
Imports Entities
Imports DataAccess
Public Class AgregarDetalleVenta
    Dim Type As String
    Dim FormaPago As Long
    Private _Item As New ITEMES
    Private _Garantia As New garantias

    Private Sub AgregarDetalleVenta_Activated(sender As Object, e As EventArgs) Handles Me.Activated
        xDato.Focus()
        Titulo()
    End Sub

    Public Sub New(ByVal xTitulo As String, Optional ByVal zFormaPago As Long = 0)

        ' Llamada necesaria para el diseñador.
        InitializeComponent()

        ' Agregue cualquier inicialización después de la llamada a InitializeComponent().
        Me.Text = xTitulo

        FormaPago = zFormaPago
        Me.xDato.Focus()
    End Sub
    Private Sub Titulo()

        With gDisponibles
            .DataSource = ItemBL.List
            .Font = New Drawing.Font("Tahoma", Util.GridFont, FontStyle.Regular, GraphicsUnit.Point)

            .Columns("ITEM").Visible = False
            .Columns("PROVEEDOR").Visible = False
            .Columns("GARANTIA").Visible = False
            .Columns("CANTIDADKILOS").Visible = False
            .Columns("PRECIOVENTA").Visible = False
            .Columns("ACTIVO").Visible = False
            .Columns("DESCRIPCION").HeaderText = "Descripción"
            .Columns("DESCRIPCION").Width = 250

            .Columns("STOCK").HeaderText = "Stock"
            .Columns("STOCK").Width = 100
        End With

        With gGarantias
            .DataSource = GarantiaBL.List
            .Font = New Drawing.Font("Tahoma", 10, FontStyle.Regular, GraphicsUnit.Point)

            .Columns("GARANTIA").Visible = False
            .Columns("CAPACIDAD").Visible = False
            .Columns("PRECIOVENTA").Visible = False

            .Columns("ACTIVO").Visible = False

            .Columns("STOCK").HeaderText = "Stock"
            .Columns("STOCK").Width = 50

            .Columns("DESCRIPCION").HeaderText = "Descripción"
            .Columns("DESCRIPCION").Width = 250
        End With

    End Sub

    Private Sub xDato_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles xDato.KeyPress
        If e.KeyChar = vbCr Then
            If validar() Then Aceptar()
        End If
        If e.KeyChar = ChrW(Keys.Escape) Then
            Me.Dispose()
        End If
        If Char.IsDigit(e.KeyChar) Then
            e.Handled = False
        ElseIf Char.IsControl(e.KeyChar) Then
            e.Handled = False
        Else
            e.Handled = True
        End If
    End Sub

    Private Sub xDato_TextChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles xDato.TextChanged

        Try
            Dim mt As Double = xDato.Text
            xDato.Text = mt.ToString("##,##")
            xDato.Select(xDato.ToString.Length, xDato.ToString.Length)
        Catch ex As Exception
        End Try
        xDato.Focus()
    End Sub

    Public Function GetValue() As Integer
        If xDato.TextLength > 0 Then
            Return CDbl(xDato.Text)
        Else
            Return 0
        End If
    End Function

    Private Function validar() As Boolean
        If xDato.Text = "" Then
            Util.Mensaje("Debe Ingresar el Precio de Venta")
            xDato.Focus()
            Return False

        End If
        If _Garantia.GARANTIA > 0 Or _Item.ITEM > 0 Then
            Return True
        Else
            Return False
        End If
    End Function

    Public Function getDetalle() As DetalleVentaR
        Dim detalle As New DetalleVentaR

        If _Garantia.GARANTIA > 0 Then
            With detalle
                .Cantidad = 1
                .GarantiaId = _Garantia.GARANTIA
                .Descripcion = _Garantia.DESCRIPCION
                .ItemId = 0
                .Valor = CLng(FormatNumber(xDato.Text))
            End With
        End If
        If _Item.ITEM > 0 Then
            With detalle
                .Cantidad = 1
                .GarantiaId = 0
                .Descripcion = _Item.DESCRIPCION
                .ItemId = _Item.ITEM
                .Valor = CLng(FormatNumber(xDato.Text))
            End With
        End If
        Return detalle
    End Function

    Private Sub bAceptar_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles bAceptar.Click
        If validar() Then Aceptar()
    End Sub

    Private Sub Aceptar()
       
        Me.DialogResult = DialogResult.OK
    End Sub

    Private Sub bCancelar_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles bCancelar.Click
        Dispose()
    End Sub

    Private Sub gGarantias_DoubleClick(ByVal sender As Object, ByVal e As System.EventArgs) Handles gGarantias.DoubleClick
        If gGarantias.Rows.IndexOf(gGarantias.CurrentRow) >= 0 Then
            If gGarantias.Item("STOCK", gGarantias.Rows.IndexOf(gGarantias.CurrentRow)).Value > 0 Then
                _Garantia = GarantiaBL.SearchById(CLng(gGarantias.Item("garantia", gGarantias.Rows.IndexOf(gGarantias.CurrentRow)).Value))
                _Item = New itemes
                lbDetalle.Text = _Garantia.DESCRIPCION
                xDato.Text = _Garantia.PRECIOVENTA
            End If
        End If
    End Sub

    Private Sub gDisponibles_DoubleClick(ByVal sender As Object, ByVal e As System.EventArgs) Handles gDisponibles.DoubleClick
        If gDisponibles.Rows.IndexOf(gDisponibles.CurrentRow) >= 0 Then
            If gDisponibles.Item("STOCK", gDisponibles.Rows.IndexOf(gDisponibles.CurrentRow)).Value > 0 Then
                _Item = ItemBL.SearchById(CLng(gDisponibles.Item("Item", gDisponibles.Rows.IndexOf(gDisponibles.CurrentRow)).Value))
                _Garantia = New garantias
                lbDetalle.Text = _Item.DESCRIPCION
                xDato.Text = _Item.PRECIOVENTA
            End If
        End If
    End Sub

    
End Class